﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

using SynergyControls;
using SynergyLib.DSP;
using SharpDX.XAudio2;

namespace XAudio2Controls
{
    public class ChorusPanel : EffectsPanel, IEffectParams
    {
        System.Windows.Controls.ComboBox voicesComboBox;

        #region Properties
        private float MaxDelayInSeconds
        {
            get { return .125f; }
        }

        /// <summary>
        /// Max feedback level in percent form.  
        /// 1.0 = 100%, 
        /// 0.56 = 56%, 
        /// etc
        /// </summary>

        private float MaxOscillatorRateInHz
        {
            get { return 1.0f; }
        }
        public float DelayMS
        {
            get
            {
                return DialAngle1 / 360 * MaxDelayInSeconds;
            }
        }
        public float Feedback
        {
            get
            {
                return DialAngle2 / 360;
            }
        }
        public float Rate
        {
            get
            {
                float rate = DialAngle3 / 360 * MaxOscillatorRateInHz;
                //string formattedRate = (Math.Round(rate, 2)).ToString() + "Hz";
                //ParamLabel3Content = formattedRate;
                return rate;
            }
        }
        public int Voices
        {
            get
            {
                return (int)voicesComboBox.SelectedValue;
            }
        }
        public ChorusEffect chorus { get; set; }
        #endregion Properties

        /// <summary>
        /// Static FlangerPanel constructor for a custom control
        /// </summary>
        static ChorusPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ChorusPanel), new FrameworkPropertyMetadata(typeof(EffectsPanel)));
        }
        /// <summary>
        /// Construct an instance of a FlangerPanel control with respect to a given SubmixVoice.
        /// </summary>
        /// <param name="submixer"></param>
        public ChorusPanel(SubmixVoice submixer, Collection<EffectDescriptor> fxChainReference)
        {
            FxChain = fxChainReference;
            EffectParams = this;
            effectType = EffectTypes.Chorus;
            Submixer = submixer;
            this.Effect = new ChorusEffect();
            chorus = (ChorusEffect)Effect;
            this.effectDescriptor = new EffectDescriptor(Effect);

       //     var border = ResourceRoot.FindName("PART_BorderParam4") as System.Windows.Controls.Border;
       //     var dial = ResourceRoot.FindName("PART_RadialDial4") as FrameworkElement;
      //      border.Child = new System.Windows.Controls.ComboBox();
            //border.Child
        }
        
        protected override void GetSpecialChildren()
        {
            var border = ResourceRoot.FindName("PART_BorderParam4") as System.Windows.Controls.Border;
            voicesComboBox = new System.Windows.Controls.ComboBox()
            {
                ItemsSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                SelectedValue = 2,
            };
            voicesComboBox.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(voicesComboBox_SelectionChanged);
            border.Child = voicesComboBox;
            ;
        }

        void voicesComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            voicesComboBox.SelectedValue = voicesComboBox.SelectedItem;
            UpdateParameters();
        }

        protected override void UpdateParameters()
        { 
            if (ParameterEnabled)
            {
                chorus.Parameters = new ChorusParams()
                {
                    DelayMS = this.Param1,
                    Feedback = this.Param2,
                    Rate = this.Param3,
                    Voices = (int)this.Param4,
                };                
            }
        }

        #region IEffectParams
        public float Param1
        {
            get
            {
                ParamLabel1Content = GetParam1Readout();
                return DelayMS;
            }
        }
        public float Param2
        {
            get
            {
                ParamLabel2Content = GetParam2Readout();
                return Feedback;
            }
        }
        public float Param3
        {
            get
            {
                ParamLabel3Content = GetParam3Readout();
                return Rate;
            }
        }
        public float Param4
        {
            get
            {
                ParamLabel4Content = GetParam4Readout();
                return Voices;
            }
        }

        public string ParamName1
        {
            get
            {
                return "Delay";
            }
        }
        public string ParamName2
        {
            get
            {
                return "Feedback";
            }
        }
        public string ParamName3
        {
            get
            {
                return "Rate";
            }
        }
        public string ParamName4
        {
            get
            {
                return "Voices";
            }
        }

        public string GetParam1Readout()
        {
            return ((Math.Round(DelayMS, 4))*1000).ToString() + "ms";
        }
        public string GetParam2Readout()
        {
            return (Math.Round(Feedback, 3) * 100).ToString() + "%";
        }
        public string GetParam3Readout()
        {
            return (Math.Round(Rate, 2)).ToString() + "Hz";
        }
        public string GetParam4Readout()
        {
            return Voices.ToString();
        }
        #endregion IEffectParams
    }
}
